package com.iweb.users.servlet;

import com.iweb.users.dao.UserDao;
import com.iweb.users.dao.UserDaoImpl;
import com.iweb.users.model.User;
import com.iweb.users.service.UserService;
import com.iweb.users.service.UserServiceImpl;
import org.apache.commons.codec.digest.DigestUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(value = "*.User")
public class UserServlet extends HttpServlet {
    UserService userService = new UserServiceImpl();
    UserDao usersdao = new UserDaoImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    // 处理请求
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=UTF-8");
        String path = req.getServletPath();
        switch (path) {
            case "/login.User":
                login(req, resp);
                break;
            case "/register.User":
                regist(req, resp);
                break;
            case "/check.User":
                checkAccount(req, resp);
                break;
            default:
                break;
        }
    }

    private void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String account = req.getParameter("account");
        String password = req.getParameter("password");
        password = DigestUtils.md5Hex(password);
        boolean isLogin = userService.isLogin(account, password, req.getSession());
        if (isLogin) {
            User user = usersdao.queryByUsernamePassword(account, password);
            Integer userAttribute = user.getUser_attribute();
            if (userAttribute == 1) {
                // 重定向到管理员页面
                resp.sendRedirect(req.getContextPath() + "/main.html");
            } else if (userAttribute == 2) {
                // 重定向到商家页面
                resp.sendRedirect(req.getContextPath() + "/welcome2.html");
            } else if (userAttribute == 3) {
                // 重定向到普通用户页面
                resp.sendRedirect(req.getContextPath() + "/welcome3.html");
            }
        } else {
            // 登录失败，重定向到登录页面并传递错误信息
            resp.sendRedirect(req.getContextPath() + "/login.html?isLogin=0");
        }
    }

    private void regist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String account = req.getParameter("account");
        boolean isNull = usersdao.isNull(account);
            String password = req.getParameter("password");
            String username = req.getParameter("username");
            String userAttributeStr = req.getParameter("user_attribute");
            Integer userAttribute = Integer.parseInt(userAttributeStr);
            password = DigestUtils.md5Hex(password);

            // 创建 User 对象
            User user = new User();
            user.setAccount(account);
            user.setPassword(password);
            user.setUsername(username);
            user.setUser_attribute(userAttribute);
            usersdao.insertUser(user);
            // 注册成功，重定向到登录页面
            resp.sendRedirect(req.getContextPath() + "/login.html");

    }

    private void checkAccount(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String account = req.getParameter("user");
        boolean isNull = usersdao.isNull(account);
        PrintWriter out = resp.getWriter();
        if (isNull) {
            out.print("{\"pass\": 1}");
        } else {
            out.print("{\"pass\": 0}");
        }
        out.close();
    }
}
